package com.attcatstudy.Thread;

/**
 * @author attcat
 * ---做一个有趣的程序员
 * @date 2022-10-31 21:30
 * @description
 */
public class WaitDemo {
    private static Object lock = new Object();
    private static boolean flag = true;

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    while (flag){
                        System.out.println("wait start .......");
                        try {
                            lock.wait();//等待
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("wait end ....... ");
            }}
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (flag){
                    synchronized (lock){
                        while (flag){
                            try {
                                Thread.sleep(5000);
                            }catch (Exception e){

                            }
//                            lock.notify();//唤醒
                            lock.notifyAll();
                            System.out.println("notify .......");
                            flag = false;
                        }
                    }
                }}
        }).start();
    }
}
